// pages/order/order.js
const app = getApp()
const tools = require('../../utils/util')
Page({
  data: {
    orderList: [],
    sum: 0,
    HZL_my_cat: [],
    allNum: 0,
    orderCode: "",
    wareList: [],
    ware_id: 0,
    userInfo: {
      avatar: '',
      user_name: ''
    },
    commentList:[],
    comment_id: 0,
    order: true,
    isCanShow: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getOrderList()
    this.getWareList()
    this.checkIsCanShow()
    this.commentList()
    console.log('HZL_my_cat', wx.getStorageSync('HZL_my_cat'))
    var HZL_my_cat = wx.getStorageSync('HZL_my_cat')
    var sum = 0;
    var allNum = 0;
    for (let i = 0; i < HZL_my_cat.length; i++) {
      sum = sum + HZL_my_cat[i].num * HZL_my_cat[i].price
      allNum = allNum + HZL_my_cat[i].num
    }
    console.log(sum)
    console.log(allNum)
    console.log('user_table--------', wx.getStorageSync('user_table'))

    // 存放订单号
    let orderCode = '';
    // 6位随机数(加在时间戳后面)
    for (var j = 0; j < 6; j++) {
      orderCode += Math.floor(Math.random() * 10);
    }
    // 时间戳(用来生成订单号)
    orderCode = 'D' + new Date().getTime() + orderCode;
    // 打印
    console.log("orderCode", orderCode)
    this.setData({
      user_table: wx.getStorageSync('user_table'),
      HZL_my_cat: wx.getStorageSync('HZL_my_cat'),
      sum: sum,
      allNum: allNum,
      orderCode: orderCode
    })
  },

  onShow: function () {
    this.getOrderList()
  },

  /**
   * 获取订单列表
   */
  getOrderList() {
    let that = this
    wx.request({
      url: 'https://order.xiaxueruo.com/api/order/list?user_id=' + wx.getStorageSync('user_id'),
      header: {
        'content-type': 'application/json'
      },
      method: 'GET', //get或者post
      success: function (res) {
        console.log(res) //接口返回值
        console.log('1111111', res.data.data.orderList)
        
        that.setData({
          orderList: res.data.data.orderList
        })
      }
    })

  },

  getWareList() {
    let that = this
    wx.request({
      url: 'https://order.xiaxueruo.com/api/ware/index',
      method: 'GET', //get或者post
      success: function (res) {
        app.globalData.wareList = res.data.data.wareList
        console.log('---app.globalData.wareList', app.globalData.wareList) //接口返回值
        for (let item of app.globalData.wareList) {
          item.add_time = tools.formatTime(item.add_time)
        }
        console.log(res) //接口返回值
        console.log('1111111', res.data.data.wareList)
        that.setData({
          wareList: app.globalData.wareList
        })
      }
    })
  },

  	/**
	 * 评论
	 */
  commentList(){
    let that = this
    wx.request({
      url: 'https://order.xiaxueruo.com/api/comment/index',
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',//get或者post
      
      success: function (res) {
        console.log(".....",res)//接口返回值
        that.setData({
          commentList: res.data.data.commentList
        })
      }
    })
  },

  getComment: function (e) {
    console.log("e",e)
    console.log('---------index', e.currentTarget.dataset)
    console.log(e.currentTarget.dataset.ware_id)
    console.log(e.currentTarget.dataset.detail_id)
    let detail_id = e.currentTarget.dataset.detail_id;
    let ware_id = e.currentTarget.dataset.ware_id;

    wx.navigateTo({
      url: '../order/orderComment/orderComment?detail_id='+detail_id+"&ware_id="+ware_id,
    })
  },

  /**
   * 获取接口数据，控制是否显示敏感信息
   */
  checkIsCanShow() {
    wx.request({
      url: 'https://order.xiaxueruo.com/api/order/isCanShow/',
      success: res => {
        this.setData({
          isCanShow: res.data
        })
      }
    })
  },
})